<?php
/**
 * CURL页面抓取  模拟数据GET与POST方法
 *
 * @since 		2009-09-10
 */

class Curl
{
	// 头信息
	public $headers;
	// 浏览器环境
	public $user_agent;
	// 处理压缩数据
	public $encoding;
	// Cookie路径
	public $cookie_file;
	// 代理配置
	public $proxy;
	// 错误信息
	public $error;

	/**
	 * 构造函数
	 *
	 * @param	bool	$cookies	是否开启Cookie进行登录模拟
	 * @param	string	$path		Cookie路径
	 * @param 	string	$encoding	解密设置
	 * @param 	string	$proxy
	 */
	public function __construct($cookies = true, $path = '', $encoding = 'gzip', $proxy = '')
	{
		$path				= 'cookies.txt';
		$this->headers[]	= 'Connection: keep-alive';
		$this->headers[]	= 'Content-type: application/x-www-form-urlencoded';
		$this->headers[]	= 'Cache-Control: no-cache';
		$this->user_agent	= 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322)';
		$this->encoding		= $encoding;
		$this->proxy		= $proxy;
		$this->cookies		= $cookies;
		if ($this->cookies == true) {
			$this->SetCookie($path);
		}
	}

	/**
	 * 设置Cookie文件
	 *
	 * @param	string	$path	文件路径
	 */
	function SetCookie($path)
	{
		if (file_exists($path))
		{
			$this->cookie_file = $path;
		} else {
			if (@fopen($path, 'w') == false) {
				$this->errMsg = 'The cookie file could not be opened. Make sure this directory has the correct permissions';
			} else {
				$this->cookie_file = $path;
			}
			@fclose($this->cookie_file);
		}
	}

	/**
	 * 普通Get方法
	 *
	 * @param 	string	$url		路径
	 * @return 	mixed	$return		结果
	 */
	public function Get($url)
	{
		$process = curl_init($url);

		curl_setopt($process, CURLOPT_HTTPHEADER, $this->headers);
		curl_setopt($process, CURLOPT_HEADER, 0);
		curl_setopt($process, CURLOPT_USERAGENT, $this->user_agent);
		curl_setopt($process, CURLOPT_COOKIE, 1);

		if ($this->cookies == true) {
			curl_setopt($process, CURLOPT_COOKIEFILE, $this->cookie_file);
		}
		if ($this->cookies == true) {
			curl_setopt($process, CURLOPT_COOKIEJAR, $this->cookie_file);
		}

		curl_setopt($process,CURLOPT_ENCODING , $this->encoding);
		curl_setopt($process, CURLOPT_TIMEOUT, 30);

		if (!empty($this->proxy)) {
			curl_setopt($cUrl, CURLOPT_PROXY, $this->proxy);
		}

		curl_setopt($process, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($process, CURLOPT_FOLLOWLOCATION, 1);

		$return = curl_exec($process);
		curl_close($process);
		return $return;
	}

	/**
	 * 普通Post方法
	 *
	 * @param 	string	$url		路径
	 * @param 	string	$data		Post数据
	 * @return 	mixed	$return		结果
	 */
	public function Post($url, $data)
	{
		$process = curl_init($url);

		curl_setopt($process, CURLOPT_HTTPHEADER, $this->headers);
		curl_setopt($process, CURLOPT_HEADER, 0);
		curl_setopt($process, CURLOPT_USERAGENT, $this->user_agent);
		curl_setopt($process, CURLOPT_COOKIE, 1);

		if ($this->cookies == true) {
			curl_setopt($process, CURLOPT_COOKIEFILE, $this->cookie_file);
		}

		if ($this->cookies == true) {
			curl_setopt($process, CURLOPT_COOKIEJAR, $this->cookie_file);
		}

		curl_setopt($process, CURLOPT_ENCODING , $this->encoding);
		curl_setopt($process, CURLOPT_TIMEOUT, 30);

		if (!empty($this->proxy)) {
			curl_setopt($cUrl, CURLOPT_PROXY, $this->proxy);
		}

		curl_setopt($process, CURLOPT_POSTFIELDS, $data);
		curl_setopt($process, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($process, CURLOPT_FOLLOWLOCATION, 1);
		curl_setopt($process, CURLOPT_POST, 1);

		$return = curl_exec($process);
		curl_close($process);
		return $return;
	}

	/**
	 * HTTPS抓取的Get方法
	 *
	 * @param 	string	$url		路径
	 * @return 	mixed	$return		结果
	 */
	public function GetSSL($url)
	{
		$process = curl_init($url);

		curl_setopt($process, CURLOPT_HTTPHEADER, $this->headers);
		curl_setopt($process, CURLOPT_HEADER, 0);
		curl_setopt($process, CURLOPT_USERAGENT, $this->user_agent);
		curl_setopt($process, CURLOPT_COOKIE, 1);

		if ($this->cookies == TRUE) {
			curl_setopt($process, CURLOPT_COOKIEFILE, $this->cookie_file);
		}
		if ($this->cookies == TRUE) {
			curl_setopt($process, CURLOPT_COOKIEJAR, $this->cookie_file);
		}

		curl_setopt($process,CURLOPT_ENCODING , $this->encoding);
		curl_setopt($process, CURLOPT_TIMEOUT, 30);

		if (!empty($this->proxy)) {
			curl_setopt($cUrl, CURLOPT_PROXY, $this->proxy);
		}

		curl_setopt($process, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($process, CURLOPT_FOLLOWLOCATION, 1);
		curl_setopt($process, CURLOPT_SSL_VERIFYPEER, false);
		curl_setopt($process, CURLOPT_SSL_VERIFYHOST, false);

		$return = curl_exec($process);
		curl_close($process);
		return $return;
	}

	/**
	 * HTTPS抓取的Post方法
	 *
	 * @param 	string	$url		路径
	 * @param 	string	$data		Post数据
	 * @return 	mixed	$return		结果
	 */
	public function PostSSL($url, $data)
	{
		$process = curl_init($url);

		curl_setopt($process, CURLOPT_HTTPHEADER, $this->headers);
		curl_setopt($process, CURLOPT_HEADER, 0);
		curl_setopt($process, CURLOPT_USERAGENT, $this->user_agent);
		curl_setopt($process, CURLOPT_COOKIE, 1);

		if ($this->cookies == TRUE) {
			curl_setopt($process, CURLOPT_COOKIEFILE, $this->cookie_file);
		}
		if ($this->cookies == TRUE) {
			curl_setopt($process, CURLOPT_COOKIEJAR, $this->cookie_file);
		}

		curl_setopt($process, CURLOPT_ENCODING , $this->encoding);
		curl_setopt($process, CURLOPT_TIMEOUT, 30);

		if (!empty($this->proxy)) {
			curl_setopt($cUrl, CURLOPT_PROXY, $this->proxy);
		}

		curl_setopt($process, CURLOPT_POSTFIELDS, $data);
		curl_setopt($process, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($process, CURLOPT_FOLLOWLOCATION, 1);
		curl_setopt($process, CURLOPT_POST, 1);
		curl_setopt($process, CURLOPT_SSL_VERIFYPEER, false);
		curl_setopt($process, CURLOPT_SSL_VERIFYHOST, false);

		$return = curl_exec($process);
		curl_close($process);
		return $return;
	}
}